package controller
{
import view.*;

import controller.NowTrack;
import controller.PlayStreamService;

import data.RadioEvents;

import data.SharedData;

import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
    import assets.radio.MainRadio;
	import flash.events.Event
	import flash.utils.Timer;
	import data.RadioEvents
	import flash.utils.SetIntervalTimer
	import flash.net.navigateToURL;
	import flash.net.URLRequest;

import view.UIMuteButton;
import view.UIPlayButton;
import view.UIVolumeControll;


/**
	 * тут и вьюха и контроллер, атата и стыдоба 
	 * View
	 * @author TrueTroll
	 */
	public class RadioController extends MainRadio
	{
		private const startVolumeLevel:int = 50;
		private var _volumeControll:UIVolumeControll;
		private var playStream:PlayStreamService;
		private var _volumeMute:UIMuteButton;
		private var _playStopButton:UIPlayButton;
		private var _nowChanel:String = "";
		private var nowTrack:NowTrack;
		private var sharedData:SharedData;
		public function RadioController()
		{	
			sharedData = new SharedData();
			
			playStream = new PlayStreamService();
			playStream.addEventListener(RadioEvents.STREAMING_PLAY, _showRadioStatus);
			playStream.addEventListener(RadioEvents.BUFFER_LOW, _showRadioStatus);
			playStream.addEventListener(RadioEvents.BUFFER_MEDIUM, _showRadioStatus);
			playStream.addEventListener(RadioEvents.STREAM_REBOOT, _showRadioStatus);
			playStream.addEventListener(RadioEvents.BUFFER_HIGHT, _showRadioStatus);
			playStream.addEventListener(RadioEvents.STREAM_ERROR, _showRadioStatus);
			
			_volumeMute = new UIMuteButton(this.muteButton);
			_volumeMute.addEventListener(RadioEvents.VOLUME_MUTE, _onVolumeChange);
			
			_volumeControll = new UIVolumeControll(this.volumeBar);
			_volumeControll.value = startVolumeLevel;
			_volumeControll.addEventListener(RadioEvents.VOLUME_CHANGED, _onVolumeChange);
			if (int(sharedData.load(SharedData.VOLUME_LEVEL)) > 0) {
				_volumeControll.value = int(sharedData.load(SharedData.VOLUME_LEVEL));
			}
			
			_playStopButton = new UIPlayButton(this.playStop);
			_playStopButton.addEventListener(RadioEvents.STREAM_PLAY_STOP_TOGGLE, _stopPlayHandler);

			this.playStop.stop();
		
			nowTrack = new NowTrack(this.currentTrackTF);
			
			_playUrl();
		}

		private function _playUrl():void
		{
			if (Config.STREAM_URL == "") _stopUberales();
			
			playStream.play(Config.STREAM_URL);
			nowTrack.enable = true;
			_onVolumeChange(null);
		}
		
		private function _stopUberales():void 
		{
            OneMorePlayer._callToJS("it is change time!");
			_clearReconectTimer();
			nowTrack.enable = false;
			playStream.removeEventListener(RadioEvents.STREAMING_PLAY, _showRadioStatus);
			playStream.removeEventListener(RadioEvents.BUFFER_LOW, _showRadioStatus);
			playStream.removeEventListener(RadioEvents.BUFFER_MEDIUM, _showRadioStatus);
			playStream.removeEventListener(RadioEvents.BUFFER_HIGHT, _showRadioStatus);
			playStream.removeEventListener(RadioEvents.STREAM_REBOOT, _showRadioStatus);
			playStream.removeEventListener(RadioEvents.STREAM_ERROR, _showRadioStatus);
		}
		
		private function _changeStation(event:Event):void
		{
			_playUrl();
		}
		
		private function _showRadioStatus(event:Event):void
		{
			//trace(event.type);
			if (event.type == RadioEvents.STREAMING_PLAY)
			{
				// start play
				this.progressBar.visible = true;
				this.currentTrackTF.visible = false;
			}
			
			// resieve ok
			if (event.type == RadioEvents.BUFFER_LOW ||
				event.type == RadioEvents.BUFFER_MEDIUM || 
				event.type == RadioEvents.BUFFER_HIGHT){
				_clearReconectTimer();
			}
			
			if (this.progressBar.visible && (event.type == RadioEvents.BUFFER_LOW || event.type == RadioEvents.BUFFER_MEDIUM || event.type == RadioEvents.BUFFER_HIGHT))
			{
				// is play
				this.progressBar.visible = false;
				this.currentTrackTF.visible = true;
			}
			if (event.type == RadioEvents.STREAM_ERROR)
			{
				_clearReconectTimer();
				_reconectTimer = new Timer(1000);
				_reconectTimer.addEventListener(TimerEvent.TIMER, _reconectFunction);
				_reconectTimer.start();
			}
			if (event.type == RadioEvents.STREAM_REBOOT)
			{
				trace("rebuild");
				playStream.stop();
				_clearReconectTimer();
				_reconectTimer = new Timer(1000);
				_reconectTimer.addEventListener(TimerEvent.TIMER, _reconectFunction);
				_reconectTimer.start();
				
			}
		}
		
		private var _reconectTimer:Timer;
		private function _clearReconectTimer():void
		{
			if(_reconectTimer){
				_reconectTimer.stop();
				_reconectTimer.removeEventListener(TimerEvent.TIMER, _reconectFunction);
			}
			_reconectTimer = null;
		}
		
		private function _reconectFunction(event:TimerEvent):void
		{
			_clearReconectTimer();
			_playUrl();
		}
		
		/**
		 * Play/Stop button handler
		 * @param	event
		 */
		private function _stopPlayHandler(event:Event):void
		{
			if (_playStopButton.state){
				_playUrl();
			}else {
				nowTrack.enable = false;
				playStream.stop();
				this.playStop.stop();
				_clearReconectTimer();
			}
		}
		
		/**
		 * Volume event handler
		 * @param	event
		 */
		private function _onVolumeChange(event:Event):void
		{
			if (event && event.type == RadioEvents.VOLUME_CHANGED) {
				_volumeMute.state = (_volumeControll.value > 0);
			}
			
			if (_volumeMute.state){
				playStream.volume = _volumeControll.value/100;
				_volumeControll.muteMode = false;
			}else{
				playStream.volume = 0;
				_volumeControll.muteMode = true;
			}
			
			sharedData.save(SharedData.VOLUME_LEVEL, _volumeControll.value.toString()); 
		}
	}
}